package pers.twf.twfRpc.server;

import io.vertx.core.Vertx;

/**
 * 基于Vert.x实现的web服务器，监听指定端口处理请求
 */
public class VertxHttpServer implements HttpServer{

    @Override
    public void doStart(int port) {
//        创建实例
        Vertx vertx = Vertx.vertx();
//        创建HTTP服务器
        io.vertx.core.http.HttpServer httpServer = vertx.createHttpServer();
/*//        监听端口并处理请求
        httpServer.requestHandler(httpServerRequest -> {
//            处理请求
            System.out.println("Received request: "+httpServerRequest.method()+" "+ httpServerRequest.uri());
//            发送HTTP响应
            httpServerRequest.response()
                    .putHeader("content-type","text/plain")
                    .end("Hello from Vetx.x HTTP server!");
        });*/
//        绑定请求处理器
        httpServer.requestHandler(new HttpServerHander());
//        启动HTTP服务器并监听指定端口
        httpServer.listen(port,httpServerAsyncResult -> {
            if (httpServerAsyncResult.succeeded()){
                System.out.println("Server is now listening on port "+port);
            }else {
                System.out.println("Failed to start server: "+ httpServerAsyncResult.cause());
            }
        });
    }
}
